var program = require('commander');
var path = require("path")
var fs = require("fs")

function list(val) {
    return val.split(',')
}

console.log(process.argv)
program
    .version('0.0.1')
    .option('-m, --model [value]', '生成model', list)
    .option('-r, --route  [value]', '生成route', list)
    .option('-s, --service  [name]', '生成service', list)
    .option('-a, --all  [name]', '生成service', list)
    .parse(process.argv)

function gen(names, fun) {
    if (typeof names == "array" || names.length) {
        for (var i in names) {
            fun(names[i])
        }
        return
    }
    if (typeof names == "String") {
        fun(names)
    }
}

function mkdirs(dir) {
    if (fs.existsSync(dir))return
    var parent = path.dirname(dir)
    if (!fs.existsSync(parent)) {
        mkdirs(parent)
    }
    fs.mkdirSync(dir)
}

function writeFile(param) {
    var file = param.path, content = param.content
    console.log("写入文件" + file)
    var _path = __dirname
    var idx = _path.indexOf(path.sep + "src" + path.sep)
    if (idx > 0) {
        _path = _path.substring(0, idx)
    }
    _path += "/src/" + file
    var dir = path.dirname(_path)
    mkdirs(dir)
    fs.writeFileSync(_path, content)
}

function genModel(name) {
    writeFile({
        path: "models/" + name + ".js",
        content: "/* global window */\n" +
        "import modelExtend from 'dva-model-extend'\n" +
        "import { config } from 'utils'\n" +
        "import { create, remove, save , query } from 'services/" + name + "'\n" +
        "import { pageModel } from './common'\n" +
        "\n" +
        "const { prefix } = config\n" +
        "\n" +
        "export default modelExtend(pageModel, {\n" +
        "  namespace: '" + name + "',\n" +
        "\n" +
        "  state: {\n" +
        "    currentItem: {},\n" +
        "    modalVisible: false,\n" +
        "    modalType: 'create',\n" +
        "    selectedRowKeys: [],\n" +
        "    isMotion: window.localStorage.getItem(`${prefix}" + name + "IsMotion`) === 'true',\n" +
        "  },\n" +
        "\n" +
        "  subscriptions: {\n" +
        "    setup ({ dispatch, history }) {\n" +
        "      history.listen((location) => {\n" +
        "        if (location.pathname === '/" + name + "') {\n" +
        "          const payload = location.query || { current: 1, pageSize: 10 }\n" +
        "          dispatch({\n" +
        "            type: 'query',\n" +
        "            payload,\n" +
        "          })\n" +
        "        }\n" +
        "      })\n" +
        "    },\n" +
        "  },\n" +
        "\n" +
        "  effects: {\n" +
        "    * query ({ payload = {} }, { call, put }) {\n" +
        "      const {data}= yield call(query, payload)\n" +
        "      console.log(data)\n" +
        "      if (data) {\n" +
        "        yield put({\n" +
        "          type: 'querySuccess',\n" +
        "          payload: {\n" +
        "            list: data.content,\n" +
        "            pagination: {\n" +
        "              current: Number(payload.page) || 1,\n" +
        "              pageSize: Number(payload.pageSize) || 10,\n" +
        "              total: data.totalElements,\n" +
        "            },\n" +
        "          },\n" +
        "        })\n" +
        "      }\n" +
        "    },\n" +
        "\n" +
        "    * delete ({ payload }, { call, put, select }) {\n" +
        "      const data = yield call(remove, { id: payload })\n" +
        "      const { selectedRowKeys } = yield select(_ => _." + name + ")\n" +
        "      if (data.success) {\n" +
        "        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })\n" +
        "        yield put({ type: 'query' })\n" +
        "      } else {\n" +
        "        throw data\n" +
        "      }\n" +
        "    },\n" +
        "\n" +
        "    * multiDelete ({ payload }, { call, put }) {\n" +
        "      const data = yield call(removes, payload)\n" +
        "      if (data.success) {\n" +
        "        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })\n" +
        "        yield put({ type: 'query' })\n" +
        "      } else {\n" +
        "        throw data\n" +
        "      }\n" +
        "    },\n" +
        "\n" +
        "    * create ({ payload }, { call, put }) {\n" +
        "      const data = yield call(save, payload)\n" +
        "      if (data.success) {\n" +
        "        yield put({ type: 'hideModal' })\n" +
        "        yield put({ type: 'query' })\n" +
        "      } else {\n" +
        "        throw data\n" +
        "      }\n" +
        "    },\n" +
        "\n" +
        "    * update ({ payload }, { select, call, put }) {\n" +
        "      const id = yield select(({ " + name + " }) => " + name + ".currentItem.id)\n" +
        "      const newUser = { ...payload, id }\n" +
        "      const data = yield call(save, newUser)\n" +
        "      if (data.success) {\n" +
        "        yield put({ type: 'hideModal' })\n" +
        "        yield put({ type: 'query' })\n" +
        "      } else {\n" +
        "        throw data\n" +
        "      }\n" +
        "    },\n" +
        "\n" +
        "  },\n" +
        "\n" +
        "  reducers: {\n" +
        "\n" +
        "    showModal (state, { payload }) {\n" +
        "      return { ...state, ...payload, modalVisible: true }\n" +
        "    },\n" +
        "\n" +
        "    hideModal (state) {\n" +
        "      return { ...state, modalVisible: false }\n" +
        "    },\n" +
        "\n" +
        "    switchIsMotion (state) {\n" +
        "      window.localStorage.setItem(`${prefix}" + name + "IsMotion`, !state.isMotion)\n" +
        "      return { ...state, isMotion: !state.isMotion }\n" +
        "    },\n" +
        "\n" +
        "  },\n" +
        "})\n"
    })
}

function firstUpcase(str) {
    var strTemp = ""; //新字符串
    for (var i = 0; i < str.length; i++) {
        if (i == 0) {
            strTemp += str[i].toUpperCase(); //第一个
            continue;
        }
        if (str[i] == " " && i < str.length - 1) { //空格后
            strTemp += " ";
            strTemp += str[i + 1].toUpperCase();
            i++;
            continue;
        }
        strTemp += str[i];
    }
    return strTemp;
}

function genRoute(name) {
    writeFile({
        path: "routes/" + name + "/Filter.js",
        content: "import React from 'react'\n" +
        "import PropTypes from 'prop-types'\n" +
        "import moment from 'moment'\n" +
        "import { FilterItem } from 'components'\n" +
        "import { Form, Button, Row, Col, DatePicker, Input, Cascader, Switch } from 'antd'\n" +
        "import city from '../../utils/city'\n" +
        "\n" +
        "const Search = Input.Search\n" +
        "const { RangePicker } = DatePicker\n" +
        "\n" +
        "const ColProps = {\n" +
        "  xs: 24,\n" +
        "  sm: 12,\n" +
        "  style: {\n" +
        "    marginBottom: 16,\n" +
        "  },\n" +
        "}\n" +
        "\n" +
        "const TwoColProps = {\n" +
        "  ...ColProps,\n" +
        "  xl: 96,\n" +
        "}\n" +
        "\n" +
        "const Filter = ({\n" +
        "  onAdd,\n" +
        "  isMotion,\n" +
        "  switchIsMotion,\n" +
        "  onFilterChange,\n" +
        "  filter,\n" +
        "  form: {\n" +
        "    getFieldDecorator,\n" +
        "    getFieldsValue,\n" +
        "    setFieldsValue,\n" +
        "  },\n" +
        "}) => {\n" +
        "  const handleFields = (fields) => {\n" +
        "    const { createTime } = fields\n" +
        "    if (createTime.length) {\n" +
        "      fields.createTime = [createTime[0].format('YYYY-MM-DD'), createTime[1].format('YYYY-MM-DD')]\n" +
        "    }\n" +
        "    return fields\n" +
        "  }\n" +
        "\n" +
        "  const handleSubmit = () => {\n" +
        "    let fields = getFieldsValue()\n" +
        "    fields = handleFields(fields)\n" +
        "    onFilterChange(fields)\n" +
        "  }\n" +
        "\n" +
        "  const handleReset = () => {\n" +
        "    const fields = getFieldsValue()\n" +
        "    for (let item in fields) {\n" +
        "      if ({}.hasOwnProperty.call(fields, item)) {\n" +
        "        if (fields[item] instanceof Array) {\n" +
        "          fields[item] = []\n" +
        "        } else {\n" +
        "          fields[item] = undefined\n" +
        "        }\n" +
        "      }\n" +
        "    }\n" +
        "    setFieldsValue(fields)\n" +
        "    handleSubmit()\n" +
        "  }\n" +
        "\n" +
        "  const handleChange = (key, values) => {\n" +
        "    let fields = getFieldsValue()\n" +
        "    fields[key] = values\n" +
        "    fields = handleFields(fields)\n" +
        "    onFilterChange(fields)\n" +
        "  }\n" +
        "  const { name, address } = filter\n" +
        "\n" +
        "  let initialCreateTime = []\n" +
        "  if (filter.createTime && filter.createTime[0]) {\n" +
        "    initialCreateTime[0] = moment(filter.createTime[0])\n" +
        "  }\n" +
        "  if (filter.createTime && filter.createTime[1]) {\n" +
        "    initialCreateTime[1] = moment(filter.createTime[1])\n" +
        "  }\n" +
        "\n" +
        "  return (\n" +
        "    <Row gutter={24}>\n" +
        "      <Col {...ColProps} xl={{ span: 4 }} md={{ span: 8 }}>\n" +
        "        {getFieldDecorator('name', { initialValue: name })(<Search placeholder=\"Search Name\" size=\"large\" onSearch={handleSubmit} />)}\n" +
        "      </Col>\n" +
        "      <Col {...ColProps} xl={{ span: 4 }} md={{ span: 8 }}>\n" +
        "        {getFieldDecorator('address', { initialValue: address })(\n" +
        "          <Cascader\n" +
        "            size=\"large\"\n" +
        "            style={{ width: '100%' }}\n" +
        "            options={city}\n" +
        "            placeholder=\"Please pick an address\"\n" +
        "            onChange={handleChange.bind(null, 'address')}\n" +
        "          />)}\n" +
        "      </Col>\n" +
        "      <Col {...ColProps} xl={{ span: 6 }} md={{ span: 8 }} sm={{ span: 12 }}>\n" +
        "        <FilterItem label=\"Createtime\">\n" +
        "          {getFieldDecorator('createTime', { initialValue: initialCreateTime })(\n" +
        "            <RangePicker style={{ width: '100%' }} size=\"large\" onChange={handleChange.bind(null, 'createTime')} />\n" +
        "          )}\n" +
        "        </FilterItem>\n" +
        "      </Col>\n" +
        "      <Col {...TwoColProps} xl={{ span: 10 }} md={{ span: 24 }} sm={{ span: 24 }}>\n" +
        "        <div style={{ display: 'flex', justifyContent: 'space-between', flexWrap: 'wrap' }}>\n" +
        "          <div >\n" +
        "            <Button type=\"primary\" size=\"large\" className=\"margin-right\" onClick={handleSubmit}>查询</Button>\n" +
        "            <Button size=\"large\" onClick={handleReset}>重置查询</Button>\n" +
        "          </div>\n" +
        "          <div>\n" +
        "            <Switch style={{ marginRight: 16 }} size=\"large\" defaultChecked={isMotion} onChange={switchIsMotion} checkedChildren={'Motion'} unCheckedChildren={'Motion'} />\n" +
        "            <Button size=\"large\" type=\"ghost\" onClick={onAdd}>新建</Button>\n" +
        "          </div>\n" +
        "        </div>\n" +
        "      </Col>\n" +
        "    </Row>\n" +
        "  )\n" +
        "}\n" +
        "\n" +
        "Filter.propTypes = {\n" +
        "  onAdd: PropTypes.func,\n" +
        "  isMotion: PropTypes.bool,\n" +
        "  switchIsMotion: PropTypes.func,\n" +
        "  form: PropTypes.object,\n" +
        "  filter: PropTypes.object,\n" +
        "  onFilterChange: PropTypes.func,\n" +
        "}\n" +
        "\n" +
        "export default Form.create()(Filter)\n"
    })
    writeFile({
        path: "routes/" + name + "/index.js", content: "import React from 'react'\n" +
        "import PropTypes from 'prop-types'\n" +
        "import { routerRedux } from 'dva/router'\n" +
        "import { connect } from 'dva'\n" +
        "import { Row, Col, Button, Popconfirm } from 'antd'\n" +
        "import { Page } from 'components'\n" +
        "import queryString from 'query-string'\n" +
        "import List from './List'\n" +
        "import Filter from './Filter'\n" +
        "import Modal from './Modal'\n" +
        "\n" +
        "\n" +
        "const " + firstUpcase(name) + " = ({ location, dispatch, " + name + ", loading }) => {\n" +
        "  location.query = queryString.parse(location.search)\n" +
        "  const { list, pagination, currentItem, modalVisible, modalType, isMotion, selectedRowKeys } = " + name + "\n" +
        "  const { pageSize } = pagination\n" +
        "\n" +
        "  const modalProps = {\n" +
        "    item: modalType === 'create' ? {} : currentItem,\n" +
        "    visible: modalVisible,\n" +
        "    maskClosable: false,\n" +
        "    confirmLoading: loading.effects['" + name + "/update'],\n" +
        "    title: `${modalType === 'create' ? '添加记录' : '编辑记录'}`,\n" +
        "    wrapClassName: 'vertical-center-modal',\n" +
        "    onOk (data) {\n" +
        "      dispatch({\n" +
        "        type: `" + name + "/${modalType}`,\n" +
        "        payload: data,\n" +
        "      })\n" +
        "    },\n" +
        "    onCancel () {\n" +
        "      dispatch({\n" +
        "        type: '" + name + "/hideModal',\n" +
        "      })\n" +
        "    },\n" +
        "  }\n" +
        "\n" +
        "  const listProps = {\n" +
        "    dataSource: list,\n" +
        "    loading: loading.effects['" + name + "/query'],\n" +
        "    pagination,\n" +
        "    location,\n" +
        "    isMotion,\n" +
        "    onChange (page) {\n" +
        "      const { query, pathname } = location\n" +
        "      dispatch(routerRedux.push({\n" +
        "        pathname,\n" +
        "        query: {\n" +
        "          ...query,\n" +
        "          page: page.current,\n" +
        "          pageSize: page.pageSize,\n" +
        "        },\n" +
        "      }))\n" +
        "    },\n" +
        "    onDeleteItem (id) {\n" +
        "      dispatch({\n" +
        "        type: '" + name + "/delete',\n" +
        "        payload: id,\n" +
        "      })\n" +
        "    },\n" +
        "    onEditItem (item) {\n" +
        "      dispatch({\n" +
        "        type: '" + name + "/showModal',\n" +
        "        payload: {\n" +
        "          modalType: 'update',\n" +
        "          currentItem: item,\n" +
        "        },\n" +
        "      })\n" +
        "    },\n" +
        "    rowSelection: {\n" +
        "      selectedRowKeys,\n" +
        "      onChange: (keys) => {\n" +
        "        dispatch({\n" +
        "          type: '" + name + "/updateState',\n" +
        "          payload: {\n" +
        "            selectedRowKeys: keys,\n" +
        "          },\n" +
        "        })\n" +
        "      },\n" +
        "    },\n" +
        "  }\n" +
        "\n" +
        "  const filterProps = {\n" +
        "    isMotion,\n" +
        "    filter: {\n" +
        "      ...location.query,\n" +
        "    },\n" +
        "    onFilterChange (value) {\n" +
        "      dispatch(routerRedux.push({\n" +
        "        pathname: location.pathname,\n" +
        "        query: {\n" +
        "          ...value,\n" +
        "          page: 1,\n" +
        "          pageSize,\n" +
        "        },\n" +
        "      }))\n" +
        "    },\n" +
        "    onSearch (fieldsValue) {\n" +
        "      fieldsValue.keyword.length ? dispatch(routerRedux.push({\n" +
        "        pathname: '/" + name + "',\n" +
        "        query: {\n" +
        "          field: fieldsValue.field,\n" +
        "          keyword: fieldsValue.keyword,\n" +
        "        },\n" +
        "      })) : dispatch(routerRedux.push({\n" +
        "        pathname: '/" + name + "',\n" +
        "      }))\n" +
        "    },\n" +
        "    onAdd () {\n" +
        "      dispatch({\n" +
        "        type: '" + name + "/showModal',\n" +
        "        payload: {\n" +
        "          modalType: 'create',\n" +
        "        },\n" +
        "      })\n" +
        "    },\n" +
        "    switchIsMotion () {\n" +
        "      dispatch({ type: '" + name + "/switchIsMotion' })\n" +
        "    },\n" +
        "  }\n" +
        "\n" +
        "  const handleDeleteItems = () => {\n" +
        "    dispatch({\n" +
        "      type: '" + name + "/multiDelete',\n" +
        "      payload: {\n" +
        "        ids: selectedRowKeys,\n" +
        "      },\n" +
        "    })\n" +
        "  }\n" +
        "\n" +
        "  return (\n" +
        "    <Page inner>\n" +
        "      <Filter {...filterProps} />\n" +
        "      {\n" +
        "        selectedRowKeys.length > 0 &&\n" +
        "        <Row style={{ marginBottom: 24, textAlign: 'right', fontSize: 13 }}>\n" +
        "          <Col>\n" +
        "            {`选择${selectedRowKeys.length} 项 `}\n" +
        "            <Popconfirm title={'确定删除这些记录?'} placement=\"left\" onConfirm={handleDeleteItems}>\n" +
        "              <Button type=\"primary\" size=\"large\" style={{ marginLeft: 8 }}>删除</Button>\n" +
        "            </Popconfirm>\n" +
        "          </Col>\n" +
        "        </Row>\n" +
        "      }\n" +
        "      <List {...listProps} />\n" +
        "      {modalVisible && <Modal {...modalProps} />}\n" +
        "    </Page>\n" +
        "  )\n" +
        "}\n" +
        "\n" +
        "Worker.propTypes = {\n" +
        "  worker: PropTypes.object,\n" +
        "  location: PropTypes.object,\n" +
        "  dispatch: PropTypes.func,\n" +
        "  loading: PropTypes.object,\n" +
        "}\n" +
        "\n" +
        "export default connect(({ " + name + ", loading }) => ({ " + name + ", loading }))(" + firstUpcase(name) + ")\n"
    })
    writeFile({
        path: "routes/" + name + "/List.js", content: "import React from 'react'\n" +
        "import PropTypes from 'prop-types'\n" +
        "import { Table, Modal } from 'antd'\n" +
        "import classnames from 'classnames'\n" +
        "import { DropOption } from 'components'\n" +
        "import { Link } from 'react-router-dom'\n" +
        "import queryString from 'query-string'\n" +
        "import AnimTableBody from 'components/DataTable/AnimTableBody'\n" +
        "\n" +
        "const confirm = Modal.confirm\n" +
        "\n" +
        "const List = ({ onDeleteItem, onEditItem, isMotion, location, ...tableProps }) => {\n" +
        "  location.query = queryString.parse(location.search)\n" +
        "\n" +
        "  const handleMenuClick = (record, e) => {\n" +
        "    if (e.key === '1') {\n" +
        "      onEditItem(record)\n" +
        "    } else if (e.key === '2') {\n" +
        "      confirm({\n" +
        "        title: '确定删除该记录?',\n" +
        "        onOk () {\n" +
        "          onDeleteItem(record.id)\n" +
        "        },\n" +
        "      })\n" +
        "    }\n" +
        "  }\n" +
        "\n" +
        "  const columns = [\n" +
        "    {\n" +
        "      title: '编号',\n" +
        "      dataIndex: 'workCode',\n" +
        "      key: 'workCode',\n" +
        "      width: 128,\n" +
        "    }, {\n" +
        "      title: '姓名',\n" +
        "      dataIndex: 'name',\n" +
        "      key: 'name',\n" +
        "      width: 240,\n" +
        "    }, {\n" +
        "      title: '电话号码',\n" +
        "      dataIndex: 'phoneNumber',\n" +
        "      key: 'phoneNumber',\n" +
        "      width: 240,\n" +
        "    }, {\n" +
        "      title: '身份证号码',\n" +
        "      dataIndex: 'idCard',\n" +
        "      key: 'idCard',\n" +
        "    }, {\n" +
        "      title: '部门',\n" +
        "      dataIndex: 'department',\n" +
        "      key: 'department',\n" +
        "    }, {\n" +
        "      title: '操作',\n" +
        "      key: 'operation',\n" +
        "      width: 100,\n" +
        "      render: (text, record) => {\n" +
        "        return <DropOption onMenuClick={e => handleMenuClick(record, e)} menuOptions={[{ key: '1', name: '修改' }, { key: '2', name: '删除' }]} />\n" +
        "      },\n" +
        "    },\n" +
        "  ]\n" +
        "\n" +
        "  const getBodyWrapperProps = {\n" +
        "    page: location.query.page,\n" +
        "    current: tableProps.pagination.current,\n" +
        "  }\n" +
        "\n" +
        "  const getBodyWrapper = (body) => { return isMotion ? <AnimTableBody {...getBodyWrapperProps} body={body} /> : body }\n" +
        "\n" +
        "  return (\n" +
        "    <div>\n" +
        "      <Table\n" +
        "        {...tableProps}\n" +
        "        bordered\n" +
        "        scroll={undefined}\n" +
        "        columns={columns}\n" +
        "        simple\n" +
        "        rowKey={record => record.id}\n" +
        "        getBodyWrapper={getBodyWrapper}\n" +
        "      />\n" +
        "    </div>\n" +
        "  )\n" +
        "}\n" +
        "\n" +
        "List.propTypes = {\n" +
        "  onDeleteItem: PropTypes.func,\n" +
        "  onEditItem: PropTypes.func,\n" +
        "  isMotion: PropTypes.bool,\n" +
        "  location: PropTypes.object,\n" +
        "}\n" +
        "\n" +
        "export default List\n"
    })
    writeFile({
        path: "routes/" + name + "/Modal.js", content: "import React from 'react'\n" +
        "import PropTypes from 'prop-types'\n" +
        "import {Form, Input, InputNumber, Radio, Modal, Cascader,DatePicker} from 'antd'\n" +
        "import city from '../../utils/city'\n" +
        "import moment from \"moment\";\n" +
        "\n" +
        "const FormItem = Form.Item\n" +
        "\n" +
        "const formItemLayout = {\n" +
        "  labelCol: {\n" +
        "    span: 6,\n" +
        "  },\n" +
        "  wrapperCol: {\n" +
        "    span: 14,\n" +
        "  },\n" +
        "}\n" +
        "\n" +
        "const modal = ({\n" +
        "                 item = {},\n" +
        "                 onOk,\n" +
        "                 form: {\n" +
        "                   getFieldDecorator,\n" +
        "                   validateFields,\n" +
        "                   getFieldsValue,\n" +
        "                 },\n" +
        "                 ...modalProps\n" +
        "               }) => {\n" +
        "  const handleOk = () => {\n" +
        "    validateFields((errors) => {\n" +
        "      if (errors) {\n" +
        "        return\n" +
        "      }\n" +
        "      const data = {\n" +
        "        ...getFieldsValue(),\n" +
        "        key: item.key,\n" +
        "      }\n" +
        "      data.birthDay=data.birthDay.toDate()\n" +
        "      onOk(data)\n" +
        "    })\n" +
        "  }\n" +
        "\n" +
        "  const modalOpts = {\n" +
        "    ...modalProps,\n" +
        "    onOk: handleOk,\n" +
        "  }\n" +
        "\n" +
        "  return (\n" +
        "    <Modal {...modalOpts}>\n" +
        "      <Form layout=\"horizontal\">\n" +
        "        <FormItem label=\"名称\" hasFeedback {...formItemLayout}>\n" +
        "          {getFieldDecorator('name', {\n" +
        "            initialValue: item.name,\n" +
        "            rules: [\n" +
        "              {\n" +
        "                required: true,\n" +
        "              },\n" +
        "            ],\n" +
        "          })(<Input/>)}\n" +
        "        </FormItem>\n" +
        "        <FormItem label=\"身份证号码\" hasFeedback {...formItemLayout}>\n" +
        "          {getFieldDecorator('idCard', {\n" +
        "            initialValue: item.idCard,\n" +
        "            rules: [\n" +
        "              {\n" +
        "                required: true,\n" +
        "              },\n" +
        "            ],\n" +
        "          })(<Input/>)}\n" +
        "        </FormItem>\n" +
        "        <FormItem label=\"性别\" hasFeedback {...formItemLayout}>\n" +
        "          {getFieldDecorator('gender', {\n" +
        "            initialValue: item.gender,\n" +
        "            rules: [\n" +
        "              {\n" +
        "                required: true,\n" +
        "                type: 'string',\n" +
        "              },\n" +
        "            ],\n" +
        "          })(\n" +
        "            <Radio.Group>\n" +
        "              <Radio value={\"Male\"}>男</Radio>\n" +
        "              <Radio value={\"Female\"}>女</Radio>\n" +
        "            </Radio.Group>\n" +
        "          )}\n" +
        "        </FormItem>\n" +
        "        <FormItem label=\"出生日期\" hasFeedback {...formItemLayout}>\n" +
        "          {getFieldDecorator('birthDay', {\n" +
        "            initialValue: moment(item.birthDay),\n" +
        "            rules: [\n" +
        "              {\n" +
        "                required: true,\n" +
        "                type: \"object\",\n" +
        "              },\n" +
        "            ],\n" +
        "          })(<DatePicker />)}\n" +
        "        </FormItem>\n" +
        "        <FormItem label=\"电话号码\" hasFeedback {...formItemLayout}>\n" +
        "          {getFieldDecorator('phoneNumber', {\n" +
        "            initialValue: item.phoneNumber,\n" +
        "            rules: [\n" +
        "              {\n" +
        "                required: true,\n" +
        "                pattern: /^1[34578]\\d{9}$/,\n" +
        "                message: '电话号码不正确!',\n" +
        "              },\n" +
        "            ],\n" +
        "          })(<Input/>)}\n" +
        "        </FormItem>\n" +
        "     </Form>\n" +
        "    </Modal>\n" +
        "  )\n" +
        "}\n" +
        "\n" +
        "modal.propTypes = {\n" +
        "  form: PropTypes.object.isRequired,\n" +
        "  type: PropTypes.string,\n" +
        "  item: PropTypes.object,\n" +
        "  onOk: PropTypes.func,\n" +
        "}\n" +
        "\n" +
        "export default Form.create()(modal)\n"
    })
}

function genService(name) {
    writeFile({
        path: "services/" + name + ".js", content: "import { request, config } from 'utils'\n" +
        "\n" +
        "const { api } = config\n" +
        "const { " + name + " } = api\n" +
        "\n" +
        "export async function query (params) {\n" +
        "  return request({\n" +
        "    url: `${" + name + "}query`,\n" +
        "    data: params,\n" +
        "  })\n" +
        "}\n" +
        "\n" +
        "export async function save (params) {\n" +
        "  return request({\n" +
        "    url: `${" + name + "}save`,\n" +
        "    data: params,\n" +
        "  })\n" +
        "}\n" +
        "\n" +
        "export async function remove (params) {\n" +
        "  return request({\n" +
        "    url: `${" + name + "}delete`,\n" +
        "    data: params,\n" +
        "  })\n" +
        "}\n" +
        "\n" +
        "export async function removes (params) {\n" +
        "  return request({\n" +
        "    url: `${" + name + "}deletes`,\n" +
        "    data: params,\n" +
        "  })\n" +
        "}\n"
    })
}

if (program.model) {
    gen(program.model, genModel)
}
if (program.route) {
    gen(program.route, genRoute)
}
if (program.service) {
    gen(program.service, genService)
}
if (program.all) {
    gen(program.all, genModel)
    gen(program.all, genRoute)
    gen(program.all, genService)
}
